import 'package:flutter/material.dart';

import '../../model/key_value_model.dart';
import '../container/separated_row.dart';
import '../radio/public_radio.dart';
import 'horizontal_item.dart';

/// FileName radio_item
///
/// @Author lax
/// @Date 2023/2/21
///
/// @Description

class RadioItem extends HorizontalItem {
  RadioItem({
    super.key,
    super.title,
    super.enable = true,
    super.required = false,
    super.padding,
    super.titleStyle,
    super.valueStyle,
    super.unableTitleStyle,
    super.unableValueStyle,
    super.titleWidth,
    super.flex,
    super.mainAxisAlignment = MainAxisAlignment.end,
    String? value,
    bool expanded = false,
    Function(KeyValueModel value)? onChanged,
    List<KeyValueModel>? dataSource,
  }) : super(
          value: enable
              ? null
              : () {
                  if ((dataSource ?? []).isEmpty) {
                    return value;
                  }
                  final list = (dataSource ?? [])
                      .where((element) => element.key == value)
                      .toList();
                  return list.isEmpty ? value : list.first.value;
                }(),
          valueWidget: enable
              ? SeparatedRow(
                  mainAxisAlignment: mainAxisAlignment,
                  expanded: expanded,
                  separator: SizedBox(width: expanded ? 0 : 16),
                  children: (dataSource ?? [])
                      .map((e) => GestureDetector(
                            onTap: () {
                              onChanged?.call(e);
                            },
                            child: Container(
                              color: Colors.transparent,
                              child: PublicRadio(
                                value: e.key == value,
                                title: e.value,
                                titleStyle: enable == true
                                    ? valueStyle
                                    : unableValueStyle,
                              ),
                            ),
                          ))
                      .toList(),
                )
              : null,
        );
}
